import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button} from 'antd';
import ExcelImport from '../common/ExcelImport';
import routes from './routes';
import fileTemplate from '../attachment/不可提报备件导入模板.xlsx';
const basePath = fetch.basePath || '';

class FileImport extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {loading: false};
    }

    onChange = () => this.setState({loading: !this.state.loading});

    onReturn = () => this.props.history.push(routes.query.url());
    
    render() {
        const description = [
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description1',
                defaultMessage: '订单类型、备件编号不能为空；'
            }),
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description2',
                defaultMessage: '订单类型 + 备件编号不可重复；'
            })
        ];

        return ([
            <div key="return" className="page-toolbar">
                <Button type="primary" onClick={this.onReturn} disabled={this.state.loading}>
                    {this.props.intl.formatMessage({
                        id: 'fileImport.btm.return',
                        defaultMessage: '返回'
                    })}
                </Button>
            </div>,
            <ExcelImport
                key="import"
                template={fileTemplate}
                action={`${basePath}/parts/api/v1/partSaleOrderTypes/import`}
                description={description}
                beforeUpload={this.onChange}
                afterUpload={this.onChange}/>
        ]);
    }
}

FileImport.propTypes = {
    history: PropTypes.object,
    intl: PropTypes.object,
};

export default injectIntl(FileImport);

